var HTTP = require('../../utils/http.js')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    carData: {
      type: "Object",
      value: Object,
      observer: function (news, olds, path) {
        
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    showJoinHope: true
  },

  /**
   * 组件的方法列表
   */
  methods: {
    join : function(){
      if (this.properties.carData.wish){
        wx.showToast({
          title: '你已添加过心愿',
          duration: 2000
        })
        return
      }
      wx.showLoading({
        title: '加载中',
      })


      const h = HTTP.request({
        method: 'POST',
        url: '/miniapp/showCar/wish',
        data: {
          token: getApp().globalData.loginInfo.token,
          id: this.properties.carData.id
        }
      })
      h.then((res) => {
        wx.showToast({
          title: '添加心愿成功',
          duration: 2000
        })
        this.properties.carData.wish = true
        var e = {
          success: true
        }
        this.triggerEvent('wishSuccess', e, '');
        wx.hideLoading()

      }, (error) => {
        wx.hideLoading()
      })
    },
    like: function(e){
      var likeState = e.currentTarget.dataset.like
      
      wx.showLoading({
        title: '加载中',
      })

      const h = HTTP.request({
        method: 'POST',
        url: likeState == 1 ? '/miniapp/showCar/like' : '/miniapp/showCar/unLike',
        data: {
          token: getApp().globalData.loginInfo.token,
          id: this.properties.carData.id
        }
      })
      h.then((res) => {
        wx.showToast({
          title: likeState == 1 ? '点赞成功' : '取消点赞成功',
          duration: 2000
        })
        likeState == 1 ? this.properties.carData.like = true : this.properties.carData.like = false
        this.setData({
          carData: this.properties.carData,
        })
        wx.hideLoading()
      }, (error) => {
        wx.hideLoading()
      })
    }
  }
})
